/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-29
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.util.WebUtils;

import com.springyouner.lottery.framework.UserSession;
import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.LotteryPhase;
import com.springyounger.lottery.dal.dataobject.LotteryPrizeQuota;
import com.springyounger.lottery.dal.dataobject.LotteryQuota;
import com.springyounger.lottery.dal.dataobject.LotteryWinner;
import com.springyounger.lottery.dal.dataobject.ResultInfo;
import com.springyounger.lottery.dal.dataobject.User;
import com.springyounger.lottery.service.user.PhaseService;

/**
 * 抽奖
 * 
 * @author springyounger
 */
public class LotteryController implements Controller {
	private static final Logger logger = LoggerFactory
			.getLogger(LotteryController.class);
	private PhaseService phaseService;

	@SuppressWarnings("unchecked")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String yourPhaseId = (String) WebUtils.getSessionAttribute(request,
				"phaseId");
		UserSession userSession = (UserSession) WebUtils.getSessionAttribute(
				request, "userSession");
		User user = userSession.getUser();
		String flag = "";
		LotteryPrizeQuota lotterPrizeQuota;
		Map model = new HashMap();
		int avaliableCount;
		int usedCount;

		String phaseId = request.getParameter("phaseId");
		if (!yourPhaseId.equalsIgnoreCase(phaseId)) {
			// 活动不对应
			logger.info("活动不对应");
			return new ModelAndView("user/lotteryError");
		}

		model.put("phaseId", phaseId);

		LotteryPhase lotteryPhase = phaseService.getPhaseById(Integer
				.parseInt(phaseId));
		if (null == lotteryPhase) {
			logger.info("活动不存在!");
			return new ModelAndView("user/lotteryError");
		}
		Date today = LotteryUtil.getToday();
		Date gmtStart = lotteryPhase.getGmtStart();
		Date gmtEnd = lotteryPhase.getGmtEnd();
		// 不在抽奖日
		if (LotteryUtil.getQuot(today, gmtStart) > 0
				|| LotteryUtil.getQuot(today, gmtEnd) < 0) {
			logger.info("活动已经过期");
			flag = "outOfPhase";
			model.put("flag", flag);
		}

		// 中奖流程处理
		LotteryWinner lotteryWinner = new LotteryWinner();
		lotteryWinner.setPhaseId(Integer.parseInt(phaseId));
		lotteryWinner.setUserId(user.getUserid());

		// 中奖信息
		ResultInfo info = phaseService.lotteryAction(lotteryWinner, today);
		if (PhaseService.WIN.equals(info.getType())) {
			// 中奖
			lotterPrizeQuota = (LotteryPrizeQuota) info.getObject();
			model.put("prize", lotterPrizeQuota);
		} else {
			flag = "noPrize";
			model.put("flag", flag);
		}

		// 答题配额更新
		LotteryQuota lotteryQuota = new LotteryQuota();
		lotteryQuota.setPhaseId(lotteryPhase.getPhaseId());
		lotteryQuota.setUserId(user.getUserid());
		lotteryQuota.setQuotaType(LotteryQuota.LOTTERY_DAILY);
		lotteryQuota.setQuotaDate(today);

		LotteryQuota quota = phaseService.getLotteryQuota(lotteryQuota);
		if (null == quota) {
			avaliableCount = lotteryPhase.getDailyQuestion();
			usedCount = 0;
		} else {
			avaliableCount = quota.getAvaliableCount();
			usedCount = quota.getQuotaAmount() - quota.getAvaliableCount();
		}

		model.put("avaliableCount", avaliableCount);
		model.put("usedCount", usedCount);
		return new ModelAndView("user/lotteryResult", model);
	}

	/**
	 * @param phaseService
	 *            the phaseService to set
	 */
	public void setPhaseService(PhaseService phaseService) {
		this.phaseService = phaseService;
	}
}
